package a20.com.yuejian_20.activity;

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.Toast;

import a20.com.yuejian_20.R;
import a20.com.yuejian_20.model.UserModel;
import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;

public class RegisterActivity extends AppCompatActivity {
    private String user;
    private String password;
    private String assure;
    private String num;
    private EditText name;
    private EditText pwd;
    private EditText assurePwd;
    private EditText phone;
    private Button register;
    private ImageView back;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.layout_register);
        initView();
        initEvent();
    }

    private void initView(){
        name = (EditText)findViewById(R.id.reg_name);
        pwd = (EditText)findViewById(R.id.reg_pwd);
        assurePwd = (EditText)findViewById(R.id.reg_pwd_assure);
        phone = (EditText)findViewById(R.id.reg_phone);
        register = (Button)findViewById(R.id.btn_reg);
        back = (ImageView)findViewById(R.id.back);
    }

    private void getData(){
        user = name.getText().toString();
        password = pwd.getText().toString();
        assure = assurePwd.getText().toString();
        num = phone.getText().toString();
    }

    private void initEvent(){
        back.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });

        register.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                getData();
                if(password.compareTo(assure)!=0){
                    Toast.makeText(getApplicationContext(),"前后密码不一致",Toast.LENGTH_SHORT).show();
                }else{
                    UserModel model = new UserModel();
                    Call<Integer> call = model.addUser(user,password,num);
                    call.enqueue(new Callback<Integer>() {
                        @Override
                        public void onResponse(Call<Integer> call, Response<Integer> response) {
                            int result = response.body();
                            Toast.makeText(getApplicationContext(),result+"",Toast.LENGTH_SHORT).show();


                        }

                        @Override
                        public void onFailure(Call<Integer> call, Throwable t) {

                        }
                    });
                }
            }
        });
    }
}
